﻿using DataBase.Utils;
using GreenXi.Model;
using PressSystem_Can.Help.Can;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using ZedGraph;
using static GreenXi.Help.Curve.ClsJudge;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;

namespace GreenXi.ViewModel
{
    public class SystemSettingsViewModel : SystemSettings
    {
        #region 数据初始化
        public SystemSettingsViewModel()
        {
            #region PLC相关
            TCPPort = GlobalVariables.TCPPort;

            DataIP = GlobalVariables.DataIP;

            DataPort = GlobalVariables.DataPort;

            DataUser = GlobalVariables.DataUser;

            DataPassword = GlobalVariables.DataPassword;

            ServoMotorIp1 = GlobalVariables.ServoMotorIp1;
            SelectedGreatestPressure = GlobalVariables.GreatestPressure;

            ServoMotorSlaveAddress = GlobalVariables.ServoMotorSlaveAddress;
            #endregion








            LanguageEn = true;
            LanguageCn = false;

            //定义操作

            CompressorsNumberChangeCommand = new DelegateCommand(() =>
            {
                OnSelectionChanged();
            });
            SwitchLanguage = new DelegateCommand(() =>
            {
                OnSwitchLanguage();
            });

        }

        //点击中英文切换
        private void OnSwitchLanguage()
        {
            var aaa = LanguageEn;
            if (LanguageEn)
            {
                UpdateLanguage(@"images/en-us.xaml");
                LanguageEn = false;
                LanguageCn = true;
            }
            else
            {
                UpdateLanguage(@"images/zh-cn.xaml");
                LanguageEn = true;
                LanguageCn = false;
            }
            //重新加载菜单栏
        }

        //压机数量改变
        private void OnSelectionChanged()
        {
            MyDbContext.Instance.SetValueByKey("CompressorsNumber", CompressorsNumberValue);
            // 处理选择值的更改
        }

        private void OnSelectionChanged2()
        {
            MyDbContext.Instance.SetValueByKey("DecimalPlaces", DecimalPlacesValue);
            // 处理选择值的更改
        }

        private void UpdateLanguage(string resourcePath)
        {
            List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();

            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                dictionaryList.Add(dictionary);
            }

            ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(resourcePath));

            Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
            Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
        }

        private string? _CompressorsNumberValue;
        public string CompressorsNumberValue
        {
            get => _CompressorsNumberValue;
            set
            {
                SetProperty(ref _CompressorsNumberValue, value);
                OnSelectionChanged();
            }
        }

        private string? _DecimalPlacesValue;
        public string DecimalPlacesValue
        {
            get => _DecimalPlacesValue;
            set
            {
                SetProperty(ref _DecimalPlacesValue, value);
                OnSelectionChanged2();
            }
        }
        #endregion
        #region 页面绑定元素
        //功能选择的选择值
        private string selectedFunctionOption;
        public string SelectedFunctionOption
        {
            get { return selectedFunctionOption; }
            set
            {
                if (selectedFunctionOption != value)
                {
                    SetProperty(ref selectedFunctionOption, value);
                    UpdateEditableOptions(); // 根据选项更新可编辑选项
                }
            }
        }

        //波特率编辑选项
        private bool baudRateEnabledOptions;
        public bool BaudRateEnabledOptions
        {
            get { return baudRateEnabledOptions; }
            set
            {
                if (baudRateEnabledOptions != value)
                {
                    //baudRateEnabledOptions = value;
                    //OnPropertyChanged(nameof(baudRateEnabledOptions));
                    SetProperty(ref baudRateEnabledOptions, value);
                }
            }
        }

        //奇偶校验编辑选项
        private bool parityEnabledOptions;
        public bool ParityEnabledOptions
        {
            get { return parityEnabledOptions; }
            set
            {
                if (parityEnabledOptions != value)
                {
                    SetProperty(ref parityEnabledOptions, value);
                }
            }
        }

        #endregion

        private void UpdateEditableOptions()
        {
            // 根据选项设置波特率和奇偶校验的可用选项
            if (SelectedFunctionOption == "Barcode")
            {
                BaudRateEnabledOptions = false;
                ParityEnabledOptions = false;
            }
            else
            {
                BaudRateEnabledOptions = true;
                ParityEnabledOptions = true;
            }
        }

     
    }
}
